﻿using ChronoScript_Compiler.Language;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChronoScript_Compiler.CodeGen
{
    public partial class CodeGen
    {
        public string GetMethodName(LanguageDefinition.Method meth)
        {
            meth.arguments.Reverse();
            string name = meth.name;
            foreach (LanguageDefinition.Declaration decl in meth.arguments)
            {
                name += "_" + decl.Type;
            }
            meth.arguments.Reverse();
            return name;
        }

        private Stack<string> breakLabels = new Stack<string>();

        public VType GuessType(LanguageDefinition.SyntaxNode sn)
        {
            if (sn is LanguageDefinition.IntLiteral)
                return VType.Dword;
            else if (sn is LanguageDefinition.StringLiteral)
                return VType.String;
            else if (sn is LanguageDefinition.Expression)
                return GuessType(sn as LanguageDefinition.SymbolRef);
            else if (sn is LanguageDefinition.SymbolRef)
            {
                return Symbols.GetType((sn as LanguageDefinition.SymbolRef).Value);
            }

            else if (sn is LanguageDefinition.FunctionCall)
            {
                LanguageDefinition.FunctionCall fc = sn as LanguageDefinition.FunctionCall;
                return getReturnType(fc.Target);
            }
            return VType.NotFound;
        }

        public VType GuessType(LanguageDefinition.Expression exp)
        {
            foreach (LanguageDefinition.SyntaxNode sn in exp.Items)
            {
                return GuessType(sn);
            }
            return VType.NotFound;
        }

        //public VType getReturnType(string name)
        //{
        //    if(Symbols.Exists(name))
        //    {
        //        return getTypeFromName(getMethod(name).type);
        //    }
        //    else
        //    {
        //        return VType.NotFound;
        //    }

        //}

        public VType getVType(string stR)
        {
            if (stR == "byte")
                return VType.Byte;
            else if (stR == "string")
                return VType.String;
            else if (stR == "int")
                return VType.Dword;
            else
                return VType.Dword;
        }

        public string GetReturnType(LanguageDefinition.FunctionCall func)
        {
            foreach (LanguageDefinition.SyntaxNode sn in AST)
            {
                if (sn is LanguageDefinition.Method)
                {
                    LanguageDefinition.Method m = sn as LanguageDefinition.Method;
                    if (m.name == func.Target)
                        return m.type;
                }
            }
            return "";
        }

        public string GetMethodName(LanguageDefinition.FunctionCall func)
        {
            string name = func.Target;
            func.Arguments.Reverse();
            foreach (LanguageDefinition.Expression exp in func.Arguments)
            {
                name += "_" + GetTypeName(GuessType(exp));
            }
            func.Arguments.Reverse();
            return name;
        }

        public string GetTypeName(VType v)
        {
            if (v == VType.Dword)
                return "int";
            else if (v == VType.String)
                return "string";
            return "object";
        }

        public VType getReturnType(string name)
        {
            if (Symbols.Exists(name))
            {
                return getTypeFromName(getMethod(name).type);
            }
            else
            {
                return VType.NotFound;
            }
        }

        public VType getTypeFromName(string name)
        {
            if (name == "string")
                return VType.String;
            else if (name == "int")
                return VType.Dword;
            else if (name == "void")
                return VType.Void;
            else
                return VType.NotFound;
        }

        public LanguageDefinition.Method getMethod(string name)
        {
            foreach (LanguageDefinition.SyntaxNode sn in AST)
            {
                if (sn is LanguageDefinition.Method)
                {
                    LanguageDefinition.Method m = sn as LanguageDefinition.Method;
                    if (m.name == name)
                        return m;
                }
            }
            return null;

        }
    }
}
